﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Optimization;
using FaWorks.WebSite.BussinessLogicLayer.Stylesheet;
using System.Xml;

namespace FaWorks.WebSite.Stylesheet
{
    public class Stylesheet
    {
        public static IHtmlString Render(string groupName)
        {
            var jg = Stylesheet.Instance.CssTable.SingleOrDefault<CssGroup>(m => m.Name == groupName);
            if (jg == null)
            {
                return Styles.Render(groupName);
            }

            var count = jg.Files.Count;
            string[] paths = new string[count];
            for (var i = 0; i < count; i++)
            {
                paths[i] = String.Format("{0}{1}ver={2}",
                    jg.Files[i].Path,
                    jg.Files[i].Path.IndexOf('?') == -1 ? "?" : "&",
                    jg.Files[i].Version);
            }

            return Styles.Render(paths);
        }



        public Stylesheet()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpRuntime.AppDomainAppPath + "bin/StylesConfig.xml");

            CssTable = new List<CssGroup>();
            string site = doc.LastChild.Attributes["site"].Value;

            foreach (XmlNode fXn in doc.SelectNodes("/Css/file"))
            {
                CssGroup jg = new CssGroup();
                jg.Files = new List<CssFile>();

                jg.Name = fXn.Attributes["name"].Value;
                CssFile jf = new CssFile();
                jf.Path = fXn.SelectSingleNode("./Path").InnerText;
                jf.Version = fXn.SelectSingleNode("./Version").InnerText;
                jf.Cdn = fXn.SelectSingleNode("./Cdn").InnerText;
                jg.Files.Add(jf);

                CssTable.Add(jg);
            }

            foreach (XmlNode gXn in doc.SelectNodes("/Css/group"))
            {
                CssGroup jg = new CssGroup();
                jg.Files = new List<CssFile>();

                jg.Name = gXn.Attributes["name"].Value;

                foreach (XmlNode fXn in gXn.SelectNodes("./file"))
                {
                    CssFile jf = new CssFile();
                    jf.Path = fXn.SelectSingleNode("./Path").InnerText;
                    jf.Version = fXn.SelectSingleNode("./Version").InnerText;
                    jf.Cdn = fXn.SelectSingleNode("./Cdn").InnerText;
                    jg.Files.Add(jf);
                }

                CssTable.Add(jg);
            }

            foreach (XmlNode cXn in doc.SelectNodes("//contain"))
            {
                string gName = cXn.InnerText;
                var jg = CssTable.Single<CssGroup>(m => m.Name == cXn.ParentNode.Attributes["name"].Value);

                foreach (CssGroup jpTemp in CssTable)
                {
                    if (jpTemp.Name == gName)
                    {
                        foreach (CssFile jfTemp in jpTemp.Files)
                        {
                            jg.Files.Add(jfTemp);
                        }
                    }
                }
            }
        }

        private IList<CssGroup> CssTable;

        private static Stylesheet _instance;

        public static Stylesheet Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Stylesheet();
                }
                return _instance;
            }
        }
    }
}